/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.usecase.editpart;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.uml2.uml.ExtensionPoint;

import com.cea.papyrus.core.PapyrusMessages;
import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editor.mouseoveraction.ActionHandleManager;
import com.cea.papyrus.core.editpart.FeatureListEditPart;
import com.cea.papyrus.core.figures.IconFigure;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.usecase.figure.ExtensionPointContainerFigure;
import com.cea.papyrus.diagram.usecase.figure.UseCaseClassifierFormFigure;
import com.cea.papyrus.diagram.usecase.figure.UseCaseEllipseGeneralFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Controller for an Extension point list edit part. 
 */
public class ExtensionPointListEditPart extends FeatureListEditPart {

	/**
	 * Creates a new ExtensionPointListEditPart, which accepts only
	 * Extension points
	 * @param graphnode the graphnode representing the feature list
	 * @param modelManager the model manager associated to the editor
	 */
	public ExtensionPointListEditPart(GraphNode graphnode,
			ModelManager modelManager) {
		super(graphnode, org.eclipse.uml2.uml.ExtensionPoint.class,
				modelManager);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.ContainerEditPart#getModelChildren()
	 */
	@Override
	protected List getModelChildren() {
		ArrayList contained= new ArrayList();
		Iterator it=getGraphNode().getContained().iterator();
		while (it.hasNext()){
			GraphElement ge=(GraphElement)it.next();
			if (!(ge instanceof GraphEdge)){
				contained.add(ge);						
			}
		}	
		return contained;	
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.FeatureListEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		return new ExtensionPointContainerFigure();
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.FeatureListEditPart#refreshVisuals()
	 */
	@Override
	protected void refreshVisuals() {		
		ExtensionPointContainerFigure f = (ExtensionPointContainerFigure) getFigure();
		f.setBackgroundColor(PapyrusPlugin.colorManager.get(getGraphNode().getContainer().getBackgroundColor()));

		IFigure parentFigure = ((GraphicalEditPart) getParent()).getFigure();

		// Shape presentation
		if (parentFigure instanceof IconFigure) {
			return;
		}	
		if (elementType == org.eclipse.uml2.uml.ExtensionPoint.class) {
			if( parentFigure instanceof UseCaseEllipseGeneralFigure){
				((UseCaseEllipseGeneralFigure) parentFigure).addExtensionPointContainer(f);
			} else if( parentFigure instanceof UseCaseClassifierFormFigure){
				((UseCaseClassifierFormFigure) parentFigure).addExtensionPointContainer(f);
			}
		}		
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.FeatureListEditPart#createEditPolicies()
	 */
	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new ExtensionPointListEditPolicy(elementType));
		installActionHandle();
	}


	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeEditPart#installActionHandle()
	 */
	@Override
	protected void installActionHandle() {
		// Install mouse hover
		ActionHandleManager actionMngr = new ActionHandleManager(this);
		try{
			actionMngr.addTool(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddExtensionPointCommand_Label
					, PapyrusMessages.AddExtensionPointCommand_Description
					, ExtensionPoint.class
					, null)
					, PositionConstants.BOTTOM);
		}
		catch (Exception e) {
			Debug.log(e);
		}
	}
}